package cn.wizzer.app.crown.modules.services.impl;

import cn.wizzer.app.crown.modules.models.CrownBet;
import cn.wizzer.app.crown.modules.models.CrownMatch;
import cn.wizzer.app.crown.modules.models.enums.BetsEnum;
import cn.wizzer.app.crown.modules.services.CrownBetService;
import cn.wizzer.app.crown.modules.services.CrownMatchService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.aop.interceptor.ioc.TransAop;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.aop.Aop;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.plugins.wkcache.annotation.CacheDefaults;
import org.nutz.plugins.wkcache.annotation.CacheRemove;
import org.nutz.plugins.wkcache.annotation.CacheRemoveAll;
import org.nutz.plugins.wkcache.annotation.CacheResult;

import java.util.List;

/**
 * 赛事关联的投注内容
 */
@IocBean(args = {"refer:dao"})
@Service(interfaceClass =CrownBetService.class)
@CacheDefaults(cacheName = "crown_bets")
public class CrownBetServiceImpl extends BaseServiceImpl<CrownBet> implements CrownBetService {
    public CrownBetServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private CrownMatchService matchService;

    @Override
    @Aop(TransAop.REPEATABLE_READ)
    public void editDo(CrownMatch match) {
        // 更新赛事是否保本
        matchService.updateIgnoreNull(match);
        // 更新投资利率
        List<CrownBet> bets = match.getBets();
        if (bets.size() > 0) {
            this.updateIgnoreNull(bets);
            this.clearCache(match.getId(), bets.get(0).getType());
        }
    }


    @Override
    @CacheResult(cacheKey = "${args[0]}&${args[1]}",  cacheLiveTime = 600)
    public List<CrownBet> queryBets(String matchId, BetsEnum betType, String orderBy) {
        // 生成缓存key：  crown_bets:6be532fb24834ebaa5fe49d6f4ae51d3&FULL_GOALS
        //              crown_bets:6be532fb24834ebaa5fe49d6f4ae51d3&FULL_PD
        //              crown_bets:6be532fb24834ebaa5fe49d6f4ae51d3&HALF_PD
        return this.query(Cnd.where("matchId", "=", matchId).and("type", "=", betType).asc("betContent"));
    }

    @Override
    @CacheRemove(cacheKey = "${args[0]}&${args[1]}")
    public void clearCache(String matchId, BetsEnum betType) {

    }

    @Override
    @CacheRemove(cacheKey = "${args[0]}&*")
    public void clearCache(String matchId) {

    }

    @Override
    @CacheRemoveAll
    public void clearCacheAll() {

    }
}
